package service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import orm.dto.ServiceTrainDTO;
import remote.Train12306Remote;
import remote.TrainTuNiuRemote;
import utils.MyDateUtils;


import java.util.List;

/**
 * @author yuyufeng
 * @date 2018/1/15
 */
@Service
public class TrainService {
    private final Logger LOG = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private TrainTuNiuRemote trainTuNiuRemote;

    @Autowired
    private Train12306Remote train12306Remote;

    public List<ServiceTrainDTO> searchTrainList(String begin, String end, String trainDate) throws Exception {
        List<ServiceTrainDTO> list;
        if (MyDateUtils.getHour() < 22 && MyDateUtils.getHour() > 8) {

            try {
                list = train12306Remote.searchTrainList(begin, end, trainDate);
            } catch (Exception e) {
                LOG.info(e.toString() + "查询train12306Remote失败，开始抓取trainTuNiuRemote " + begin + " " + end + " " + trainDate);
                list = trainTuNiuRemote.searchTrainList(begin, end, trainDate);
            }
        } else {
            list = trainTuNiuRemote.searchTrainList(begin, end, trainDate);
        }
        return list;
    }

    public static void main(String[] args) throws Exception {
        TrainService trainService = new TrainService();
        List<ServiceTrainDTO> list = trainService.searchTrainList("杭州", "贵阳", "2018-02-14");
        for (ServiceTrainDTO serviceTrainDTO : list) {
            System.out.println(serviceTrainDTO);
        }
    }
}

